<?php
namespace App\Http\ViewComposers;

use App\Models\SystemSetting;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Models\Group;
use Illuminate\Support\Facades\Config;
use App\Models\InboxDialog;
/**
 *
 * @author Joker
 *        
 */
class FrontComposer
{
    private $infos;
    private $authUser;
    private $baidukey;
    private $unread = 0;
    public function __construct()
    {
        $systemSetting = new SystemSetting();
        $this->infos = $systemSetting->getSetting("index");
        $this->authUser = Auth::user();
        if(!empty($this->authUser))
        {
            $userRoles = array_fetch($this->authUser->roles->toArray(),'id');
            $group_id = DB::table('group_role')->whereIn('role_id',$userRoles)->lists('group_id');
            $usergroups = Group::whereIn('id',$group_id)->first();
            $this->authUser->group_id = !empty($usergroups)?$usergroups->id:0;
            $sender_unread = InboxDialog::where('sender_uid','=',$this->authUser->id)->sum('sender_unread');
            $recipent_unread = InboxDialog::where('recipient_uid','=',$this->authUser->id)->sum('recipient_unread');
            $this->unread = $sender_unread+$recipent_unread;
        }
        $this->baidukey = Config::get('app.baidukey');
    }
    public function compose(View $view)
    {
        $view->with('systeminfo',$this->infos);
        $view->with('authuser',$this->authUser);
        $view->with('baidukey',$this->baidukey);
        $view->with('unread',$this->unread);
    }
}